# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gtk

from AdventureMoney.core import ShareProfile

import InterfaceUtils
import AdvancedShareDialog

class AdvancedShareWidget(gtk.VBox):
	def __init__(self, account, parentWindow, total=None):
		gtk.VBox.__init__(self)
	
		self.account = account
		self.total = total
		self.parentWindow = parentWindow
		self.sharedWidgetsDict = {}
		self.shareProfile = ShareProfile.ShareProfile()
		self.updating = False
		self.set_spacing(6)
		
		self.buttonHBox = gtk.HButtonBox()
		self.buttonHBox.set_layout(gtk.BUTTONBOX_SPREAD)
		self.buttonHBox.set_spacing(12)
		
		self.invertButton = gtk.Button("Invert Selections")
		self.invertButton.connect("clicked", self.OnInvertShared)
		self.buttonHBox.add(self.invertButton)
		
		self.advancedButton = gtk.Button("Advanced...")
		self.advancedButton.connect("clicked", self.OnAdvanced)
		self.buttonHBox.add(self.advancedButton)
		
		self.sharesErrorLabel = gtk.Label()
		self.sharesErrorLabel.set_use_markup(True)
		self.sharesErrorLabel.set_line_wrap(True)
		self.sharesErrorLabel.show()
		
		if self.account.getPeople():
			self.pack_end(self.buttonHBox, False, False)
		else:
			self.pack_start(gtk.Label("This account has no people."))
		
		for person in self.account.getPeople():
			check = gtk.CheckButton(person.getName())
			check.connect("toggled", self.OnCheckToggled)
			label = gtk.Label()
			self.sharedWidgetsDict[person] = (check, label)
			
			hbox = gtk.HBox()
			hbox.pack_start(check)
			hbox.pack_end(label, False, False)
			self.pack_start(hbox)
			
		self.show_all()
	
	def OnCheckToggled(self, checkbox):
		if self.updating:
			return
		
		name = checkbox.get_label()
		if checkbox.get_active():
			shareType = ShareProfile.SHARE_EQUAL
		else:
			shareType = ShareProfile.SHARE_NONE
		
		person = self.account.findPerson(name)
		self.SetPersonShare(person, shareType)
		
	def OnInvertShared(self, widget):
		for person, (checkbox, label) in self.sharedWidgetsDict.iteritems():
			shareType, value = self.shareProfile.getPersonShare(person)
			if shareType == ShareProfile.SHARE_NONE:
				self.shareProfile.setPersonShare(person, ShareProfile.SHARE_EQUAL)
			else:
				self.shareProfile.setPersonShare(person, ShareProfile.SHARE_NONE)
		self.UpdateShares()
	
	def OnAdvanced(self, widget):
		AdvancedShareDialog.AdvancedShareDialog(self.parentWindow, self, self.account)
	
	def GetPersonShare(self, person):
		return self.shareProfile.getPersonShare(person)
	
	def SetPersonShare(self, person, shareType, value=None):
		self.shareProfile.setPersonShare(person, shareType, value)
		self.UpdateShares()
		
	def GetProfile(self):
		return self.shareProfile
	
	def SetProfile(self, profile):
		self.shareProfile = profile
		self.UpdateShares()
		
	def SetTotal(self, total):
		self.total = total
		self.UpdateShares()
		
	@InterfaceUtils.UpdatingGUI
	def UpdateShares(self):
		for checkbox, label in self.sharedWidgetsDict.itervalues():
			label.set_text("")
			checkbox.set_active(False)
		
		if len(self.shareProfile) == 0:
			# nothing to calculate
			return
		
		if self.total is not None:
			msg = None
			try:
				sharesDict = self.shareProfile.calculateShares(self.total)
			except ShareProfile.OverTotalException:
				msg = "The sum is greater than the total value."
			except ShareProfile.UnderTotalException:
				msg = "The sum is less than the total value."
			else:
				if self.sharesErrorLabel.parent:
					self.remove(self.sharesErrorLabel)
				
				for person, (checkbox, label) in self.sharedWidgetsDict.iteritems():
					if not person in sharesDict:
						continue
					value = sharesDict[person]
					label.set_text("%.2f" % value)
					checkbox.set_active(True)
			
			if msg:
				msg = '<span foreground="red">%s</span>' % msg
				self.sharesErrorLabel.set_markup(msg)
				if not self.sharesErrorLabel.parent:
					self.pack_end(self.sharesErrorLabel)
		else:
			#if we have no total value, such as when using by a category
			for person, (checkbox, label) in self.sharedWidgetsDict.iteritems():
				shareType, value = self.shareProfile.getPersonShare(person)
				if shareType != ShareProfile.SHARE_NONE:
					checkbox.set_active(True)
					
				if shareType == ShareProfile.SHARE_EQUAL:
					label.set_text("Equal Portion")
				elif shareType == ShareProfile.SHARE_EXACT:
					label.set_text("%.2f" % value)
				elif shareType == ShareProfile.SHARE_PERCENT:
					label.set_text("%d%%" % value)
